package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.RoleDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 角色管理
 */

@Service(interfaceClass = RoleService.class)
@Transactional
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleDao roleDao;

    /**
     * 新增角色
     *
     * @param role
     * @param permissionIds
     * @param menuIds
     */
    //新增角色,同时让角色关联权限和菜单
    public void add(Role role, Integer[] permissionIds, Integer[] menuIds) {
        //新增角色
        roleDao.add(role);
        //设置角色和权限,菜单的多对多关系
        Integer roleId = role.getId();
        this.setRoleAndPermission(roleId, permissionIds);
        this.setRoleAndMenu(roleId, menuIds);
    }

    //建立角色和权限多对多的关系
    public void setRoleAndPermission(Integer roleId, Integer[] permissionIds) {

        if (permissionIds != null && permissionIds.length > 0) {
            for (Integer permissionId : permissionIds) {
                Map<String, Integer> map = new HashMap<>();
                map.put("roleId", roleId);
                map.put("permissionId", permissionId);
                roleDao.setRoleAndPermission(map);
            }
        }
    }

    //建立角色和菜单多对多的关系
    public void setRoleAndMenu(Integer roleId, Integer[] menuIds) {

        if (menuIds != null && menuIds.length > 0) {
            for (Integer menuId : menuIds) {
                Map<String, Integer> map = new HashMap<>();
                map.put("roleId", roleId);
                map.put("menuId", menuId);
                roleDao.setRoleAndMenu(map);
            }
        }
    }
//分页查询
    public PageResult findPage(QueryPageBean queryPageBean) {

        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();

        PageHelper.startPage(currentPage, pageSize);
        Page<Role> page = roleDao.findByCondition(queryString);
        List<Role> rows = page.getResult();
        long total = page.getTotal();
        return new PageResult(total, rows);


    }

    @Override
    public Role findById(Integer id) {

        return roleDao.findById(id);
    }

  //权限id
    public List<Integer> findPermissionIdsByRoleId(Integer id) {
        return roleDao.findPermissionIdsByRoleId(id);
    }

   //菜案id
    public List<Integer> findMenuIdsByRoleId(Integer id) {
        return roleDao.findMenuIdsByRoleId(id);
    }

 //编辑角色
 public void edit( Role role,Integer[] permissionIds,Integer[] menuIds) {
     //1.修改检查组基本信息
    roleDao.edit(role);

     //2.清理当前角色关联的权限
     //2.清理当前角色关联的菜单
     Integer roleId = role.getId();
     roleDao.delete(roleId);
     roleDao.delete1(roleId);
     //3.重新建立当前角色和权限的关联关系
     //3.重新建立当前角色和菜单的关联关系
     this.setRoleAndPermission(roleId,permissionIds);
this.setRoleAndMenu(roleId,menuIds);
 }

}
